<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class isp_komplain_status extends MY_Controller {
	
	function __construct()
	{
		parent::__construct(); 
		
		$this->load->model('isp_komplain_status_model', 'IspKomplain');
		//$this->load->model('isp_combo_model', 'Combo');
		$this->secure_session();
	}
	
	function index(){
		$data['title'] = "Setup Status Komplain ";	
		viewByTemplate('isp_komplain_status/index', $data, 'grid');
	}

	function id_auto(){
		$query = $this->db->query("SELECT id_status FROM (SELECT * FROM mis_komplain_status ORDER BY id_status DESC) WHERE rownum <=1");
	 				if ($query->row_array()) {
	 					$getquery = $query->row_array();
	 					$rowcount = $getquery['id_status'];
	 					$autonum = $rowcount + 1;
	 				} else {
					$autonum = 1;
				}
		$data['id_status_auto'] = $autonum;

		$query = $this->db->query("SELECT kode_status FROM (SELECT * FROM isp_komplain_status ORDER BY kode_status DESC) WHERE rownum <= 1");
		$getquery = $query->row_array();

		if($getquery)
			$MaxID = $getquery['kode_status'];
		else
			$MaxID = "ST0000000";

		$NextID = (int) substr($MaxID,2,7);

		$NextID++;
		
		$data['kode_status_auto'] = "ST" . sprintf("%07s",$NextID);

		echo json_encode($data);
	}
	
	function getdata()
	{
		$this->secure_session();
		echo $this->IspKomplain->GetJSON();
	}

	private function validation()
	{
		$error = new ErrorList();

		// Get Error Template
		$tpl_empty = $this->MessageTemplate("ER001");
		$tpl_duplicate = $this->MessageTemplate("ER002");


		// Validation		
		if (post("kode_status") == '') {
			$error->Add("kode_status", "Kode status", $tpl_empty);
		} else if (post("mode") == "create" && intval($this->get_data("SELECT 1 FROM isp_komplain_status WHERE kode_status = '".@ereg_replace("[^a-zA-Z0-9_-]", "",@strtoupper(@trim(post("kode_status"))))."'")) > 0) {
			$error->Add("kode_status", "Kode status", $tpl_duplicate);
		} else if (post("nama_status") == '') {
			$error->Add("nama_status", "Nama status", $tpl_empty);
		}

		return $error;
	}
	
	function ajax_post()
	{
		$is_error = 0;
		$ls_response = "";
		$ls_genid = "";

		$error = $this->validation();
		try
		{
			if (count( $error->Get() ) > 0) { $is_error = 1; }
			else
			{
				// Fill Post Data
				$data	= array( 'id_status' => $this->input->post('id_status'),
								 'kode_status' => $this->input->post('kode_status'),
								 'nama_status' => $this->input->post('nama_status')
								 );
				//$data['kode_status']	= @ereg_replace("[^a-zA-Z0-9_-]", "",@strtoupper(@trim($data['kode_status'])));
				
				if (post("mode") == "create")
				{
					//$data['create_date']	= NOW;
					//$data['create_user']	= USERID;

					$this->IspKomplain->Add($data);

					$ls_response = "Data Tersimpan";
					//App.audit_trail("CS001", "create/" + (data.role_id ?? ""));

				}
				else if (post("mode") == "edit")
				{
					//$data['modify_date']	= NOW;
					//$data['modify_user']	= USERID;
					
					$this->IspKomplain->Update($data, 
										array('id_status'	=> $data['id_status']));

					$ls_response = "Data Tersimpan[Edited]";
					//App.audit_trail("CS002", "edit/" + (data.role_id ?? ""));

				}
			}
		} catch(Exception $e) {
			$is_error = 1; $ls_response = $e->getMessage();
		}
		
		echo $this->PostResponse(json_encode(array(	'is_error'	=> $is_error,
													'fields'	=> $error->GetDictionary(),
													'msg'		=> $ls_response,
													'genid'		=> $ls_genid,
													'form_sender'=> post("form_sender"))
								));
	}

	function ajax_delete()
	{
		$ls_result		= "";
		$pesan 			= "Data Terhapus";
		$id_status 	= get('id_status');
		
		if ($id_status != "")
		{
			// Delete Single
			$arr_rule	= array('id_status' => $id_status);
			$ls_result = $this->IspKomplain->DeleteSingle($arr_rule);

		}
		else
		{
			
			$num_row = intval($this->data['num_row']['deleted']);
			
			for ($i = 0; $i < $num_row; $i++)
			{
				$row	= $this->data['deleted'][$i];
				
				$arr_rule	= array('id_status' => $row['id_status']);
				$ls_result .= $this->IspKomplain->DeleteSingle($arr_rule);
			}
		}
		
		echo $pesan;
		echo "
			<script>
				loadPage(gs_path + '/' + controller + '/index');
			</script>
		";
	}
}
?>